import Vue from 'vue';
import VueI18n from 'vue-i18n';
import Cookies from "js-cookie";  //引入
import enLocale from 'element-ui/lib/locale/lang/en';
import zhLocale from 'element-ui/lib/locale/lang/zh-CN';
import en from './en'
import zh from './zh'
Vue.use(VueI18n);

const messages = {
  en: {
    ...en,
    ...enLocale
  },
  zh: {
    ...zh,
    ...zhLocale
  }
};
export function getLanguage () {
    // Read the language of the last page closed
    const chooseLanguage = Cookies.get('language')
    if (chooseLanguage) return chooseLanguage
  
    // if has not choose language
    const language = (
      navigator.language || navigator.browserLanguage
    ).toLowerCase()
    const locales = Object.keys(messages)
    for (const locale of locales) {
      if (language.indexOf(locale) > -1) {
        return locale
      }
    }
    return Config.language
  }
const i18n = new VueI18n({
  locale:getLanguage(), // set locale
  messages // set locale messages
});

export default i18n;